<?php
class Node extends Module {
    private $types;
    
    
    // -- act start
    public function act_add() {
        $type_id = tos_http_get('type_id');
        if (empty($type_id)) {
            return;
        }
        
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            if ($node_id = $this->_add_node_data($type_id)) {
                $submit = tos_http_get('submit');
                if ($submit == 'Save') {
                    m('app')->redirect(array(
                        'm' => 'node',
                        'a' => 'edit',
                        'node_id' => $node_id
                    ));
                } else {
                    m('app')->redirect(array(
                        'm' => 'node',
                        'a' => 'display',
                        'node_id' => $node_id
                    ));
                }
            }
        }
        
        $out = out();
        
        
        $out->add_prejs(tos_cdn_url('js/z.js'));
        $out->add_prejs(tos_cdn_url('js/net.js'));
        $out->add_prejs(tos_cdn_url('js/cdn.js'));
        $out->add_prejs(tos_cdn_url('js/calendar.js'));
        $out->add_prejs(tos_cdn_url('js/knm.js'));
        $out->add_prejs(tos_cdn_url('js/richtext.js'));
        
        
        $out->add_prejs_script($this->view('js/setting'));
        $out->add_css(tos_cdn_url('css/cdn.css'));
        
        
        $type = $this->_get_type($type_id);
        
        $page_caption = 'Add ' . $type['name'];
        $data = array('page_caption' => $page_caption);
        
        $parent_id = tos_http_get('parent_id');
        $keyword_id = tos_http_get('keyword_id');
        
        if ( ! empty($parent_id)) {
            $parent_node = $this->model('node')->get_entry($parent_id);
            $data['parent_node'] = $parent_node;
        }
        
        
        $block_node_edit = $this->view('node_edit', $data);
        
        $out->add_title($page_caption);
        
        if ( 1 === (int)$type['has_content']) {
            $block_node_content = $this->view("node/{$type['name']}_edit");
            $block_node_edit->add_block($block_node_content);
        }
        
        $out->add_block($block_node_edit);
        
    }
    
    
    public function act_edit() {
        $node_id = (int) tos_http_get('node_id');
        if ($node_id <= 0) {
            tos_die();
        }
    
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            if ($this->_edit_node_data($node_id, tos_http_get('type_id'))) {
                $submit = tos_http_post('submit');
                if ($submit == 'Save') {
                    m('app')->redirect(array(
                        'm' => 'node',
                        'a' => 'edit',
                        'node_id' => $node_id
                    ));
                } else {
                    m('app')->redirect(array(
                        'm' => 'node',
                        'a' => 'display',
                        'node_id' => $node_id
                    ));
                }
            }
        }
        
        $out = out();
        
        $out->add_prejs(tos_cdn_url('js/z.js'));
        $out->add_prejs(tos_cdn_url('js/net.js'));
        $out->add_prejs(tos_cdn_url('js/cdn.js'));
        $out->add_prejs(tos_cdn_url('js/calendar.js'));
        $out->add_prejs(tos_cdn_url('js/knm.js'));
        $out->add_prejs(tos_cdn_url('js/richtext.js'));
        
        $out->add_prejs_script($this->view('js/setting'));
        $out->add_css(tos_cdn_url('css/cdn.css'));
        
        
        
        $node_model = $this->model('node');
        $data = $node_model->get_entry($node_id);
        
        $type = $this->_get_type($data['type_id']);
        $page_caption = 'Edit ' . $type['name'];
        $data['page_caption'] = $page_caption;
        
        $node_keywords = $node_model->get_node_keywords($node_id);
        $prev_node_mapping = $node_model->get_prev_node_mapping($node_id);
        $data['node_keywords'] = $node_keywords;
        $data['prev_node_mapping'] = $prev_node_mapping;
        $data['action'] = 'edit';
        
        $block_node_edit = $this->view('node_edit', $data);
        
        $out->add_title($page_caption);
        
        if ( 1 === (int)$type['has_content']) {
            $type_name = $type['name'];
            $data = $node_model->get_content($type_name, $node_id);
            $block_node_content = $this->view("node/{$type['name']}_edit", $data);
            $block_node_edit->add_block($block_node_content);
        }
        
        
        
        $out->add_block($block_node_edit);
        
    }
    
    
    public function act_display() {
        $node_id = (int) tos_http_get('node_id');
        if ($node_id <= 0) {
            tos_die('error request');
        }
        
        $node_model = $this->model('node');
        $node = $node_model->get_entry($node_id);
        $type = $this->_get_type($node['type_id']);
        
        
        /*$page_caption = 'Edit ' . $type['name'];
        $data['page_caption'] = $page_caption;*/
        
        $node_keywords = $node_model->get_node_keywords($node_id);
        $prev_node_mapping = $node_model->get_prev_node_mapping($node_id);
        $node['node_keywords'] = $node_keywords;
        $node['prev_node_mapping'] = $prev_node_mapping;
        $node['action'] = 'display';
        
        
        $block_node_display = $this->view('node_display', $node);
        
        $out = out();
        $out->add_prejs(tos_cdn_url('js/z.js'));
        $out->add_prejs(tos_cdn_url('js/knm.js'));
        $out->add_prejs_script($this->view('js/setting'));
        
        $out->add_title($node['caption']);
        $out->add_block($block_node_display);
        
        if ( 1=== (int) $type['has_content']) {
            $node_content_data = $node_model->get_content_entry($node_id, $type['name']);
            $block_node_content = $this->view("node/{$type['name']}_display", $node_content_data);
            $block_node_display->add_block($block_node_content);
        }
        
    }
    
    
    public function act_dashboard() {
        $out = out();
        $out->add_title('Dashboard');
        $out->add_prejs(m('cdn')->url('js/z.js'));
        
        $keywords = tos_http_get('q');
        $prev_id = (int) tos_http_get('prev_id');
        $next_id = (int) tos_http_get('next_id');
        $offset = (int) tos_http_get('offset');
        $limit = (int) tos_http_get('limit');
        $type_id = (int) tos_http_get('type_id');
        
        $nodes = $this->model('node')->get_entries($keywords, $prev_id, $next_id,$type_id, $offset, $limit);
        $dashboard = $this->view('dashboard', 
            array('nodes' => $nodes, 'types' => $this->_get_types()));
        
        
        
        $out->add_block($dashboard);
    }
    // -- act end
    
    // --- block start
    public function block_add_options($base_url = '') {
        $base_url = (string) $base_url;
        $base_url = trim($base_url);
        $base_url = (empty($base_url)) ? tos_url(array('m'=>'node', 'a'=>'add')) : $base_url;
        
        
        $data = array(
            'type_list' => $this->_get_types(),
            'base_url'  => $base_url
        );
        return $this->view('add_options', $data);
    }
    
    // --- block end
    
    
    // --- private function start
    private function _get_types() {
        if ( ! isset($this->types)) {
            $this->types = $this->model('node')->get_types();
        }
        return $this->types;
    }
    
    
    private function _get_type($type_id) {
        $types = $this->_get_types();
        return isset($types[$type_id]) ? $types[$type_id] : false;
    }
    
    
    private function _add_node_data($type_id = 0) {
        $type_id = (int) $type_id;
        if ($type_id <= 0) {
            tos_die('error request for add node data');
        }
        $caption = tos_http_post('caption');
        if (empty($caption)) {
            tos_die('caption cannot be none');
        }
        $description = tos_http_post('description');
        $description = tos_html_strip($description);
        $keywords = tos_http_post('keyword');
        $url_segment = tos_http_post('url_segment');
        $node_thumbnail = tos_http_post('node_thumbnail');
        
        
        $node_model = $this->model('node');
        $type_fields = $node_model->get_type_fields($type_id);
        $types = $this->_get_types();
        $type = $types[$type_id];
        
        $data = array();
        $data_format = array();
        
        //print_r($type_fields);
        
        foreach ($type_fields as $field) {
            $k = $field['name'];
            $v = isset($_POST[$k]) ? $_POST[$k] : false;
            
            if (1 == $field['required'] && $v === false) {
                if ($field['has_default'] == 1) {
                    $v = $field['default'];
                } else {
                    tos_die("$k cannot be none");
                }
            }
            
            if ($field['is_description'] && empty($description)) {
                $description = tos_html_strip($v, 200);
            }
            
            if ($v !== false) {
                $data[$k] = $v;
                $data_format[$k] = $field['db_data_type'];
            }
        }
        
        $user_id = m('user')->get_user_id();
        $access_mode = NODE_ACCESS_PRIVATE;
        
        $node_id = $node_model->add_node(
            $user_id, $type_id, $caption, $keywords, $description, $url_segment, $access_mode, $node_thumbnail
        );
        if ($node_id <= 0) {
            tos_die('add node failed');
        }
        
        $prev_nm = tos_http_post('prev');
        
        foreach ($prev_nm as $k => $ns) {
            $_keyword = isset($keywords[$k]) ? $keywords[$k] : '';
            foreach ($ns as $_node_id) {
                $node_model->add_node_mapping($_node_id, $node_id, $_keyword);
            }
        }
        
        $data['node_id'] = $node_id;
        $data_format['node_id'] = DB_DATA_INT;
        
        $content_id = $node_model->add_content($type['name'], $data, $data_format);
        if ($content_id <= 0) {
            tos_die('add node content failed');
        } else {
            return $node_id;
        }
        //$nm_id = $this->_add_prev_nm(tos_http_post('parent_id'), $node_id, tos_http_get('keyword'));
        //if ($nm_id <= 0) tos_die();
        
        return false;
    }
    
    
    private function _edit_node_data($node_id = 0, $type_id = 0) {
        //print_r($_POST);
        $node_id = (int) $node_id;
        if ($node_id <= 0)  tos_die();
        
        $caption = tos_http_post('caption');
        $description = tos_http_post('description');
        $description = tos_html_strip($description);
        //$keywords = tos_http_post('keyword');
        $url_segment = tos_http_post('url_segment');
        $node_thumbnail = tos_http_post('node_thumbnail');
        
        $node_model = $this->model('node');
        
        $result = $node_model->edit_node(
            $node_id, $caption, $description, $url_segment, 0, $node_thumbnail
        );
        
        if ($result) {
            $type_fields = $node_model->get_type_fields($type_id);
            //$types = $this->_get_types();
            $type = $this->_get_type($type_id);
            $data = array();
            foreach ($type_fields as $field) {
                $k = $field['name'];
                $v = isset($_POST[$k]) ? $_POST[$k] : false;
                
                if ($v !== false) {
                    $data[$k] = $v;
                    $data_format[$k] = $field['db_data_type'];
                }
            }
            if ($data)
                return $node_model->edit_content($type['name'], $node_id, $data, $data_format);
            return true;
        } else {
            return false;
        }
    }
    
    
    private function _add_prve_nm($prev_id, $node_id, $keyword = '') {
        $nm_id = $this->model('node')->add_node_mapping($prev_id, $next_id, $keyword);
        return $nm_id;
    }
    
    // --- private function end
}
?>